/**
 * 入驻商商品新增和编辑公用JS文件
 */


/**
 * [skuAdd sku属性组合列表添加内容]
 * @param  {objerc} dom       操作dom对象
 * @param  {array} attribute 商品属性分类数组
 * @param  {array} skuAttr  后台传回历史参数记录
 * @param  {number} type      状态 1 新增, 2编辑
 */
function skuAdd(dom, attribute, skuAttr, type) {
	var joinStr = jointStr(attribute);

	attrIdStr = joinStr.attrIdStr;
	attrNameStr = joinStr.attrNameStr;

	var stairsName = [{ str: '一级价', name: 'price_one' }, { str: '二级价', name: 'price_two' }, { str: '三级价', name: 'price_three' }]; //存储所选择阶梯价格

	$('#attribute-list').empty();
	var oldSkuAttr = skuAttr; //历史选择
	var oldSkuAttrValue = [];

	for (var i = 0; i < attrIdStr.length; i++) {
		var id = i + 1;
		var gdataId = $('#cost-stairs .active input[type=radio]').attr('gdata-id'); //阶梯价格选择ID
		var imgSrc1 = ""; //图片地址1
		var imgSrc2 = ""; //图片地址2
		var shop_sku_sn = ""; //商家编码
		var inventory = ""; //sku组合库存
		var stairsHtml = []; //商品属性中价格输入框html
		var price = ""; //价格
		var wholesale_retail_price = ""; //建议零售价
		var editTypeStr = ""; //列表勾选
		var wholesale_min_retail_price = ''; //最低零售价

		var _attrIdStr = attrIdStr[i].length == 0 ? attrIdStr[i] : attrIdStr[i].split('|').sort().join("|"); //当前组合属性id组合
		// 查找指定ID数据并赋数据
		for (var k = 0; k < oldSkuAttr.length; k++) {
			if (oldSkuAttr[k].attr_str.split('|').sort().join("|") === _attrIdStr) {
				imgSrc1 = oldSkuAttr[k].sku_img;
				imgSrc2 = oldSkuAttr[k].sku_img2;
				price = oldSkuAttr[k].price ? oldSkuAttr[k].price : "";
				shop_sku_sn = oldSkuAttr[k].shop_sku_sn ? oldSkuAttr[k].shop_sku_sn : '';
				inventory = oldSkuAttr[k].inventory ? oldSkuAttr[k].inventory : "";
				wholesale_min_retail_price = oldSkuAttr[k].wholesale_min_retail_price ? oldSkuAttr[k].wholesale_min_retail_price : '';
				wholesale_retail_price = oldSkuAttr[k].wholesale_retail_price;
			}
		}

		// 商品匹配价格个数
		for (var j = 0; j < gdataId; j++) {
			var _price = "";
			if (price instanceof Array) {
				_price = price[j];
			} else {
				_price = "";
			}

			var strHtml = '<input type="text" class="js-attr-input-' + stairsName[j].name + '" name="' + stairsName[j].name + '[]" value="' + _price + '" placeholder="' + stairsName[j].str + '" maxlength="11" disabled>';
			stairsHtml.push(strHtml);
		}

		//新增
		if(type === 1) {
			editTypeStr =
			'		<span class="scheckbox form-input" title="' + attrNameStr[i] + '">' +
			'			<i></i>'+
			'			<input type="checkbox" class="shide" name="attr_str[]" value="' + attrIdStr[i] + '" data-description="' + attrNameStr[i] + '">' + attrNameStr[i] +
			'		</span>';
		}
		//编辑
		else if (type === 2) {
			editTypeStr =
			'		<span class="scheckbox form-input" title="'+attrNameStr[i]+'">'+
			'			<i></i>'+
			'           <input type="hidden" name="sku_id[]" data-selector="sku-id" value=""/>'+
			'			<input type="checkbox" class="shide" name="attr_str[]" value="' + attrIdStr[i] + '" data-description="' + attrNameStr[i] + '">' + attrNameStr[i] +
			'		</span>';
		}

		var upload = '<a href="javascript:;" class="img-upload">' +
			'	 <i class="round-delete"></i>' +
			'    <input type="file" name="file" accept="image/jpg,image/jpeg,image/gif,image/png" id="sku-uploadimage-file' + id + '" class="uploadimage-file hide"  onchange="uploadImg(this, 2)"/>' +
			'    <input type="hidden" name="sku_img[]" class="js-attr-upload" value="' + imgSrc1 + '" disabled>' +
			'    <div id="sku-uploadimage-loading' + id + '" class="hide">Uploading...</div>' +
			'    <p>上传图片</p>' +
			'    <div id="sku-uploadimage-preview' + id + '" class="uploadimage-preview" onclick="document.querySelector(\'#sku-uploadimage-file'+id+'\').click()">' +
			'       <img id="sku-uploadimage-img' + id + '" src="' + imgSrc2 + '">' +
			'    </div>' +
			'</a>';

		dom.append(
			'<tr>' +
			'    <td class="sku-list-title">' +
					editTypeStr +
			'    </td>' +
			'    <td>' +
			'        <input type="text" class="form-control input-sm js-attr-input-sn" name="goods_sn[]" value="' + shop_sku_sn + '" maxlength="20" disabled>' +
			'    </td>' +
			'	 <td>' + stairsHtml.join('') + '</td>' +
			'	 <td><input type="text" class="js-attr-input-inventory" name="inventory[]" placeholder="库存量" value="' + inventory + '" maxlength="6" disabled></td>' +
			'	 <td><input type="text" class="js-attr-input-wholesale-min-retail-price" name="wholesale_min_retail_price[]" placeholder="最低零售价" value="' + wholesale_min_retail_price + '" maxlength="20" disabled></td>' +
			'	 <td><input type="text" class="js-attr-input-retail-price" name="wholesale_retail_price[]" placeholder="建议零售价" value="' + wholesale_retail_price + '" maxlength="20" disabled></td>' +
			'    <td>' +
			'        <div class="attr-upload-img">' +
			upload +
			'        </div>' +
			'    </td>' +
			'</tr>');
	}
	var newSkuAttr = $('#attribute-list .scheckbox input[type=checkbox]'); //已有属性sku组合 dom
	var newSkuAttrValue = []; //sku属性组合列表
	//输出新SKU 组合数组
	for (var i = 0; i < newSkuAttr.length; i++) {
		var _arr = $(newSkuAttr[i]).val().split("|");
		_arr = _arr.sort();
		newSkuAttrValue.push(_arr);
	}
	for (var i = 0; i < oldSkuAttr.length; i++) {
		oldSkuAttrValue.push(oldSkuAttr[i].attr_name);
	}

	editAttrAdd(newSkuAttrValue, oldSkuAttrValue, newSkuAttr, skuAttr);

	return { attrIdStr: attrIdStr, attrNameStr: attrNameStr };
}


/**
 * [editAttrAdd description]
 * @param  {array} arr1    新sku组合数组
 * @param  {array} arr2    旧sku组合数组
 * @param  {object} attrDom sku DOM对象
 * @param  {array} skuAttr 后台传回历史参数记录
 */
function editAttrAdd(arr1, arr2, attrDom, skuAttr) {
	var gdataId = $('#cost-stairs .active input[type=radio]').attr('gdata-id'); //阶梯价格选择ID
	if (arr1.length > arr2.length) {
		var _arr = [];
		for (var i = 0; i < arr2.length; i++) {
			for (var k = 0; k < arr1.length; k++) {
				if (arr1[k].sort().toString() === arr2[i].sort().toString()) {
					if(_arr.length !== 0) {
						if (_arr.indexOf(arr2[i]) === -1) {
							_arr.push({ id: k, val: arr2[i] });
						}
					}
					else {
						_arr.push({ id: k, val: arr2[i] });
					}
				} else {
					attrDom.eq(k).siblings('input[type=hidden]').val('-1');
					attrDom.eq(k).prop('checked', false);
					attrDom.eq(i).parent().removeClass('active');
					attrDom.eq(k).parents('tr').find('input[type=text]').prop('disabled', true).val("");
					attrDom.eq(k).parents('tr').find('.js-attr-upload').prop('disabled', true);
				}
			}
		}
		for (var i = 0; i < _arr.length; i++) {
			for (var k = 0; k < skuAttr.length; k++) {
				if (skuAttr[k].attr_str.split("|").sort().join("|") == _arr[i].val.join("|")) {
					attrDom.eq(_arr[i].id).prop('checked', true);
					attrDom.eq(_arr[i].id).parent().addClass('active');
					attrDom.eq(_arr[i].id).siblings('input[type=hidden]').val(skuAttr[k].sku_id);
					attrDom.eq(_arr[i].id).parents('tr').find('input[type=text]').prop('disabled', false);
					attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-upload').prop('disabled', false);

					attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-sn').val(skuAttr[k].shop_sku_sn);
					attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-inventory').val(skuAttr[k].inventory);
					attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-retail-price').val(skuAttr[k].wholesale_retail_price);
					attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-wholesale-min-retail-price').val(skuAttr[k].wholesale_min_retail_price);

					if (+gdataId === 1) {
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[k].price_one);
					} else if (+gdataId === 2) {
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[k].price_one);
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[k].price_two);
					} else if (+gdataId === 3) {
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[k].price_one);
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[k].price_two);
						attrDom.eq(_arr[i].id).parents('tr').find('.js-attr-input-price_three').val(skuAttr[k].price_three);
					}
				}
			}
		}
	}

	if (arr1.length < arr2.length) {
		var _arr = [];
		for (var i = 0; i < arr1.length; i++) {
			for (var k = 0; k < arr2.length; k++) {
				if (arr1[i].sort().toString() === arr2[k].sort().toString()) {
					if (_arr.length !== 0) {
						if (_arr.indexOf(arr1[i]) === -1) {
							_arr.push({ id: i, val: arr1[i] });
						}
					} else {
						_arr.push({ id: i, val: arr1[i] });
					}
				} else {
					attrDom.eq(k).siblings('input[type=hidden]').val('-1');
					attrDom.eq(k).prop('checked', false);
					attrDom.eq(i).parent().removeClass('active');
					attrDom.eq(k).parents('tr').find('input[type=text]').prop('disabled', true).val("");
					attrDom.eq(k).parents('tr').find('.js-attr-upload').prop('disabled', true);
				}
			}
		}
		for (var i = 0; i < skuAttr.length; i++) {
			for (var k = 0; k < _arr.length; k++) {
				if (skuAttr[i].attr_str.split('|').sort().join("|") == _arr[k].val.join("|")) {
					attrDom.eq(_arr[k].id).prop('checked', true);
					attrDom.eq(_arr[k].id).parent().addClass('active');
					attrDom.eq(_arr[k].id).siblings('input[type=hidden]').val(skuAttr[i].sku_id);
					attrDom.eq(_arr[k].id).parents('tr').find('input[type=text]').prop('disabled', false);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-upload').prop('disabled', false);

					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-sn').val(skuAttr[i].shop_sku_sn);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-inventory').val(skuAttr[i].inventory);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-retail-price').val(skuAttr[i].wholesale_retail_price);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-wholesale-min-retail-price').val(skuAttr[i].wholesale_min_retail_price);


					if (+gdataId === 1) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
					} else if (+gdataId === 2) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[i].price_two);
					} else if (+gdataId === 3) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[i].price_two);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_three').val(skuAttr[i].price_three);
					}

				}
			}
		}
	}

	if (arr1.length === arr2.length) {
		var _arr = [];
		for (var i = 0; i < arr1.length; i++) {
			for (var k = 0; k < arr2.length; k++) {
				if (arr1[i].sort().toString() === arr2[k].sort().toString()) {
					if(_arr.length !== 0) {
						if (_arr.indexOf(arr1[i]) === -1) {
							_arr.push({ id: i, val: arr1[i]});
						}
					}
					else {
						_arr.push({ id: i, val: arr1[i]});
					}
				} else {
					attrDom.eq(k).siblings('input[type=hidden]').val('-1');
					attrDom.eq(k).prop('checked', false);
					attrDom.eq(i).parent().removeClass('active');
					attrDom.eq(k).parents('tr').find('input[type=text]').prop('disabled', true).val("");
					attrDom.eq(k).parents('tr').find('.js-attr-upload').prop('disabled', true);
				}
			}
		}

		for (var i = 0; i < skuAttr.length; i++) {
			for (var k = 0; k < _arr.length; k++) {
				if (skuAttr[i].attr_str.split('|').sort().join("|") == _arr[k].val.join("|")) {
					attrDom.eq(_arr[k].id).prop('checked', true);
					attrDom.eq(_arr[k].id).parent().addClass('active');
					attrDom.eq(_arr[k].id).siblings('input[type=hidden]').val(skuAttr[i].sku_id);
					attrDom.eq(_arr[k].id).parents('tr').find('input[type=text]').prop('disabled', false);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-upload').prop('disabled', false);

					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-sn').val(skuAttr[i].shop_sku_sn);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-inventory').val(skuAttr[i].inventory);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-retail-price').val(skuAttr[i].wholesale_retail_price);
					attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-wholesale-min-retail-price').val(skuAttr[i].wholesale_min_retail_price);

					if (+gdataId === 1) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
					} else if (+gdataId === 2) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[i].price_two);
					} else if (+gdataId === 3) {
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_one').val(skuAttr[i].price_one);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_two').val(skuAttr[i].price_two);
						attrDom.eq(_arr[k].id).parents('tr').find('.js-attr-input-price_three').val(skuAttr[i].price_three);
					}

				}
			}
		}
	}

}



//存储属性组合列表
/**
 * [saveListAttr description]
 * @param  {object} dom      操作DOM
 * @param  {array} SKU_JSON 后台传回历史参数记录
 * @param  {array} skuAttr  自存历史参数记录
 * @return {object}         SKU_JSON, skuAttr
 */
var saveListAttr = function(dom, SKU_JSON, skuAttr) {
	var parentDom = dom;
	var id = parentDom.index();
	var _SKU_JSON = SKU_JSON
	var _skuAttr = skuAttr;
	var _activeAttrId = []; //已选元素id

	var _sku_json = function(_SKU_JSON) {
		var _arr = [];
		var _sku = SKU_JSON;

		for (var i = 0; i < _sku.length; i++) {
			_arr.push({
				id: i,
				sku_id: _sku[i].sku_id,
				attr_name: _sku[i].attr_name,
				attr_str: _sku[i].attr_str,
				attr_name_val: _sku[i].attr_name_val,
				shop_sku_sn: _sku[i].shop_sku_sn,
				price_one: _sku[i].price_one,
				price_two: _sku[i].price_two,
				price_three: _sku[i].price_three,
				price: _sku[i].price,
				inventory: _sku[i].inventory,
				wholesale_retail_price: _sku[i].wholesale_retail_price,
				wholesale_min_retail_price: _sku[i].wholesale_min_retail_price,
				sku_img: _sku[i].sku_img,
				sku_img2: _sku[i].sku_img2
			});

			_activeAttrId.push(_sku[i].attr_name.sort(sortNumber).toString());
		}

		return _arr;
	}();

	var attr_str = parentDom.find('input[type=checkbox]').val();
	var attr_name_val = ("" + parentDom.find('input[type=checkbox]').data("description")).split(" | ").join("|");
	var shop_sku_sn = parentDom.find('.js-attr-input-sn').val();

	var price_one = parentDom.find('.js-attr-input-price_one').val();
	var price_two = parentDom.find('.js-attr-input-price_two').val();
	var price_three = parentDom.find('.js-attr-input-price_three').val();
	var price = [price_one, price_two, price_three];

	var m_goods_price = parentDom.find('.js-attr-input-price').val();
	var inventory = +parentDom.find('.js-attr-input-inventory').val();
	var sku_img = parentDom.find('.js-attr-upload').val();
	var sku_img2 = parentDom.find('.uploadimage-preview img').attr('src');
	var attr_name = ("" + parentDom.find('input[type=checkbox]').val()).split("|").sort();
	var wholesale_retail_price = parentDom.find('.js-attr-input-retail-price').val();
	var wholesale_min_retail_price = parentDom.find('.js-attr-input-wholesale-min-retail-price').val();

	var _data = {
		id: id,
		attr_name: attr_name,
		attr_str: attr_str,
		attr_name_val: attr_name_val,
		shop_sku_sn: shop_sku_sn,
		price_one: price_one,
		price_two: price_two,
		price_three: price_three,
		price: price,
		inventory: inventory,
		wholesale_retail_price: wholesale_retail_price,
		wholesale_min_retail_price: wholesale_min_retail_price,
		sku_img: sku_img,
		sku_img2: sku_img2
	};

	// 排序函数
	function sortNumber(a, b) {
		return a - b;
	}

	if (skuAttr.length === 0) {
		if (_data.id !== -1 && attr_str !== undefined && shop_sku_sn !== undefined && sku_img !== undefined && sku_img2 !== undefined) {
			if ($.inArray(_data.attr_name.sort(sortNumber).toString(), _activeAttrId) === -1) {
				_skuAttr.push(_data);
			}
		}

	} else {
		var _arr = _skuAttr;
		var _arrLen = _arr.length

		for (var i = 0; i < _arrLen; i++) {
			if (id == _arr[i].id) {
				_arr.splice(i, 1, _data);

			} else {
				if (_data.id !== -1 && attr_str !== undefined && shop_sku_sn !== undefined && sku_img !== undefined && sku_img2 !== undefined) {
					_arr.push(_data);
				}
			}
		}
		_skuAttr = _arr;

	}

	var skuLen = _sku_json.length; //历史数据
	var skuAttrLen = skuAttr.length; //新数据
	var hasPos = []; //存储重复pos 位置
	var _skuAllAttr = _skuAttr.concat(_sku_json); //合成后参数
	var _skuAllLen = _skuAllAttr.length;
	var _skuId = []; //数据ID
	var _outStr = []; //去重后数据

	for (var i = 0; i < _skuAllLen; i++) {
		var _str = _skuAllAttr[i].attr_name.sort(sortNumber).toString();
		if($.inArray(_str, _skuId) === -1) {
			_skuId.push(_str);
		}
	}
	$.each(_skuId, function(i, el) {
		if ($.inArray(el, _outStr) === -1) {
			_outStr.push(_skuAllAttr[i]);
		}
		// else {
		// 	_skuAllAttr.splice(i, 1);
		// }
	});

	return { "skuAttr": _outStr }
}


// //商品参数栏中添加新属性
// $(document).on('click', '#parameter-attr-add',function(){
// 	if($('.shop-table-parameter tbody tr').length < 100) {
// 		$('.shop-table-parameter tbody').append(
// 				'<tr>'+
// 				'    <input type="hidden" value="2" name="grade[]">'+
// 				'    <input type="hidden" value="" name="param_id[]"/>'+
// 				'    <td class="fore1">'+
// 				'        <input type="text" name="param_name[]" placeholder="参数名称" maxlength="50">'+
// 				'    </td>'+
// 				'    <td class="fore2">'+
// 				'        <div>'+
// 				'            <input type="text" name="param_value[]" placeholder="参数值" maxlength="50">'+
// 				'            <a href="javascript:;" class="color-danger js-attr-delete">删除</a>'+
// 				'        </div>'+
// 				'    </td>'+
// 				'</tr>'
// 		);
// 	}
// });

// //商品参数栏中删除属性
// $(document).on('click', '.js-attr-delete',function(e){
// 	$(this).parents("tr").remove();
// });
//商品参数栏中添加新属性
    var new_param_id=1;
    $('#parameter-attr-add').click(function() {
        var tmp_id="new"+new_param_id;
        new_param_id++;
        if($('.shop-table-parameter tbody tr').length < 100) {
            $('.shop-table-parameter>tbody').append(
                '<tr class="new-tr">'+
                '    <td>'+
                '        <input type="text" name="param_name" placeholder="一级参数名称" onblur="blurfn(this)" maxlength="50" aria-invalid="false" class="valid"> </td>'+
                '    <td class="fore2">'+
                '     <input type="hidden" value="'+tmp_id+'" name="pId">'+
                '     <input type="hidden" value="" name="pName"  class="pName" />'+
                '        <table class="sub-table2">'+
                '            <tbody>'+
                '                <tr>'+
                '                <input type="hidden" value="'+tmp_id+'" name="parentId[]">'+
                '                <input type="hidden" value="" name="parentName[]" class="parentName"/>'+
                '                <input type="hidden" value="" name="secId[]"/>'+
                '                    <td>'+
                '                        <input type="text" name="secName[]" placeholder="二级参数名称" maxlength="50">'+
                '                    </td>'+
                '                        <td >'+
                '                            <div>'+
                '                                <input type="text" name="paramValue[]" placeholder="参数值" maxlength="50">'+
                '                            </div>'+
                '                        </td>'+
                '                        <td class="operate">'+
                '                           <a  href="javascript:;" class="sub-param-add2 color-info">添加二级参数</a>'+
                '                        <a href="javascript:;" class="color-danger sub-attr-delete-row">删除</a>'+
                '                        </td>'+
                '                </tr>'+
                '            </tbody>'+
                '        </table>'+
                '        </td>'+
                '</tr>'
            );
        }
    });
function blurfn(m) {
    $(m).closest("td").siblings("td").find(".pName").val(m.value);
    $(m).closest("td").siblings("td").find("table").find(".parentName").val(m.value);
 }
    $(document).on("click",".sub-param-add",function () {
        var _pid=$(this).closest(".sub-table").siblings("input[name=pId]").val()||"";
        var _pname=$(this).closest(".sub-table").siblings("input[name=pName]").val()||"";
                    $(this).closest('tbody').append(
                    '<tr>'+
                    '    <input type="hidden" value="'+_pid+'" name="parentId[]">'+
                    '    <input type="hidden" value="'+_pname+'" name="parentName[]"/>'+
                    '    <input type="hidden" value="" name="secId[]"/>'+
                    '    <td class="new-td1">'+
                    '        <input type="text" name="secName[]" placeholder="二级参数名称" maxlength="50">'+
                    '    </td>'+
                    '    <td class="new-td2">'+
                    '        <div>'+
                    '            <input type="text" name="paramValue[]" placeholder="参数值" maxlength="50">'+
                    '            <a href="javascript:;" class="color-danger js-sub-attr-delete">删除</a>'+
                    '        </div>'+
                    '    </td>'+
                    '</tr>'
            );
    })

        $(document).on("click",".sub-param-add2",function () {
        var _pid2=$(this).closest(".sub-table2").siblings("input[name=pId]").val()||"";
        var _pname2=$(this).closest(".sub-table2").siblings("input[name=pName]").val()||"";
                    $(this).closest('tbody').append(
                    '<tr>'+
                    '    <input type="hidden" value="'+_pid2+'" name="parentId[]">'+
                    '    <input type="hidden" value="'+_pname2+'" name="parentName[]"  class="parentName"/>'+
                    '    <input type="hidden" value="" name="secId[]"/>'+
                    '    <td class="new-td1">'+
                    '        <input type="text" name="secName[]" placeholder="二级参数名称" maxlength="50">'+
                    '    </td>'+
                    '    <td class="new-td2">'+
                    '        <div>'+
                    '            <input type="text" name="paramValue[]" placeholder="参数值" maxlength="50">'+
                    '            <a href="javascript:;" class="color-danger js-sub-attr-delete">删除</a>'+
                    '        </div>'+
                    '    </td>'+
                    '</tr>'
            );
    })

    //商品参数栏中删除属性
    $(document).on('click', '.js-attr-delete',function(e){
        $(this).parents('tr').remove();
    });
    $(document).on("click",".js-sub-attr-delete",function () {
        $(this).closest("tr").remove();
    })

    $(document).on('click', '.sub-attr-delete-row',function(e){
        $(this).closest('tr.new-tr').remove();
    });

//最低起批量选择
$(document).on('click', '#min-num .scheckbox i',function(){
	var parentDom = $(this).parent();

	parentDom.siblings('.scheckbox').find('div.error').remove();
	parentDom.siblings('.scheckbox').find('input[type=text]').removeClass('error').val("");

	if(parentDom.hasClass('active')) {

		parentDom.siblings().removeClass('active');
		parentDom.find('input[type=radio]').prop("checked", true);
		parentDom.siblings('.scheckbox').find('input[type=radio]').prop("checked", false);

		var state = $(this).siblings('input[type=radio]').val();
		//起批量为台时
		if(state === '1') {
			$(this).siblings('input[type=text]').attr("name", "sale_conditions").prop('disabled', false);

			parentDom.siblings('div').find('input[type=text]').prop('disabled', true).val("");
			parentDom.siblings('.scheckbox').find('input[type=text]').attr("name","").prop('disabled', true).val("");
		}
		//起批量为件时
		else if(state === '2') {
			$(this).siblings('input[type=text]').attr("name", "sale_conditions").prop('disabled', false);

			parentDom.siblings('div').find('input[type=text]').prop('disabled', false).val("");
			parentDom.siblings('.scheckbox').find('input[type=text]').attr("name","").prop('disabled', true).val("");
		}
	}
	else {
		$(this).parent().addClass('active');
		parentDom.find('input[type=radio]').prop("checked", true);
		parentDom.siblings('.scheckbox').find('input[type=radio]').prop("checked", false);
	}
});

// 起批数量为台时，失去输入框焦点，设置起始数量
$(document).on('blur', '#min-num input[name=sale_conditions]',function(){
	var state = +$('#min-num .active').find('input[type=radio]').val();
	var _v = +$("#min-num .active input[type=text]").val(); //起批件数
	if(state === 1) {
		$('.cost-stairs-right .cost-grade').eq(0).find('input[name=lvl1_start]').val(_v);
	}
	else if(state === 2) {
		var sale_unit = +$('#min-num input[name=sale_unit]').val();
		$('.cost-stairs-right .cost-grade').eq(0).find('input[name=lvl1_start]').val(_v * sale_unit);
	}
});

//最低起批价格选择起批单位是件时
$(document).on('blur', '#min-num input[name=sale_unit]',function(){
	var state = +$('#min-num .active').find('input[type=radio]').val();
	var startNum = +$("#min-num .active input[type=text]").val(); //起批件数
	var _v = startNum * $(this).val();

	if(state === 2) {
		$('.cost-stairs-right .cost-grade').eq(0).find('input[name=lvl1_start]').val(_v);
	}
});

//是否支持混批
$(document).on('click', '#isMixture .scheckbox i',function(){
	var parentDom = $(this).parent();

	if(parentDom.hasClass('active')) {
		var state = +parentDom.find('input[type=radio]').val();

		parentDom.siblings().removeClass('active');
		parentDom.find('input[type=radio]').prop("checked", true);
		parentDom.siblings('.scheckbox').find('input[type=radio]').prop("checked", false);

		if(state === 1) {
			$(this).parent().siblings('select').prop('disabled', false);
		}
		else if(state === 0) {
			$(this).parent().siblings('select').prop('disabled', true);
		}
	} else {
		$(this).parent().addClass('active');
		parentDom.find('input[type=radio]').prop("checked", true);
		parentDom.siblings('.scheckbox').find('input[type=radio]').prop("checked", false);

	}
});

//表单验证
$("#goods-form").validate({
	rules: {
		goods_name: {
			required: true
		},
		sale_point: {
			required: true
		},
		shop_spu: {
			required: false
		},
		sale_conditions: {
			required: true,
			digits: true,
			min: 1
		},
		sale_unit: {
			number: true,
			min: 0
		},
		brand_id: {
			required: true
		},
		sale_con_one: {
			number: true,
			min: 1
		},
		sale_con_two: {
			number: true,
			min: 1
		},
		volume: {
			required: true,
			number: true,
			min: 0.0001
		},
		weight: {
			required: true,
			number: true,
			min: 0.0001
		}

	},
	messages: {
		goods_name: {
			required: '请输入商品名称'
		},
		sale_point: {
			required: '请输入商品卖点'
		},
		sale_conditions: {
			required: '请输入最低起批数量',
			digits: '只能输入整数',
			min: '最少起批量只能为1台'
		},
		sale_unit: {
			number: "请输入正确的起批台数",
			min: "起批台数不能为0"
		},
		brand_id: {
			required: '请选择商品品牌'
		},
		sale_con_one: {
			number: "请输入正确的价格",
			min: 1
		},
		sale_con_two: {
			number: "请输入正确的价格",
			min: 1
		},
		volume: {
			required: '请输入单个商品体积',
			number: '参数只能为数字',
			min: '体积不能少于0.0001'
		},
		weight: {
			required: '请输入单个商品重量',
			number: '参数只能为数字',
			min: '重量不能少于0.0001'
		}
	},
	errorElement: "div",
	errorPlacement: function(error, element) {
		$(element)
				.parent()
				.append(error);
	},
});
